import axios from 'axios'
import router from '@/router'
import JSONbig from 'json-bigint'
import { Message } from 'element-ui'
// import store from '@/store/'
const isPro = process.env.NODE_ENV === 'production'
const request = axios.create({
  baseURL: isPro ? 'https://autumnfish.cn' : '/api',
  transformResponse: [function (data) {
    try {
      return JSONbig.parse(data)
    } catch (err) {
      return data
    }
  }]
})

// request.interceptors.request.use(
//   function (config) {
//     const { user } = store.state
//     if (user) {
//       config.headers.Authorization = `Bearer ${user.token}`
//     }
//     return config
//   },
//   function (error) {
//     return Promise.reject(error)
//   }
// )

request.interceptors.request.use(
  function (response) {
    return response
  },
  function (error) {
    if (error.response && error.response.status === 401) {
      window.localStorage.removeItem('user')
      Message('登陆过期,请重新登陆')
      router.push('/login')
    } else if (error.response.status === 400) {
      Message.warning('请求参数错误')
    } else if (error.response.status === 403) {
      Message.error('无操作权限')
    } else if (error.response.status >= 500) {
      Message.error('服务器异常,请稍后重试')
    }
    return Promise.reject(error)
  }

)

export default request
